//
//  LoginTextView.m
//  QuickReadComic
//
//  Created by 苗钧豪 on 16/8/27.
//  Copyright © 2016年 苗钧豪. All rights reserved.
//

#import "LoginTextView.h"
#import "JHMacro.h"
@implementation LoginTextView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 设置颜色
        self.backgroundColor = [UIColor clearColor];
        [self setUp];
    }
    return self;
}
// 初始化控件
- (void)setUp {
    // 动画图片
    self.imgView = [[UIImageView alloc]initWithFrame:CGRectMake(30, 0, self.bounds.size.width - 60, 120)];
    self.imgView.image = [UIImage imageNamed:@"1"];
    [self addSubview:self.imgView];
    // 子视图
    self.SunView = [[UIView alloc]initWithFrame:CGRectMake(0, 120, self.bounds.size.width, self.bounds.size.height - 120)];
    [self addSubview:self.SunView];
    // 分割线
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, self.bounds.size.height - (self.SunView.bounds.size.height / 2), self.SunView.bounds.size.width, 1)];
    view.backgroundColor = [UIColor grayColor];
    [self addSubview:view];
    // 手机号图片
    self.nickNameImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, self.SunView.bounds.size.height / 2)];
    self.nickNameImageView.image = [UIImage imageNamed:@"IMG_0419"];
    [self.SunView addSubview:self.nickNameImageView];
    // 手机号码
    self.nickNameField = [[UITextField alloc]initWithFrame:CGRectMake(50, 0, self.SunView.bounds.size.width - 50, self.SunView.frame.size.height / 2)];
    _nickNameField.placeholder = @"您的手机号";
    _nickNameField.delegate = self;
    _nickNameField.backgroundColor = [UIColor whiteColor];
    _nickNameField.clearButtonMode = UITextFieldViewModeAlways;
    _nickNameField.keyboardType = UIKeyboardTypePhonePad;
    [self.SunView addSubview:self.nickNameField];
    // 密码图片
    self.passWordImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.SunView.frame.size.height / 2 , 50, self.SunView.bounds.size.height / 2)];
    self.passWordImageView.image = [UIImage imageNamed:@"IMG_0420"];
    [self.SunView addSubview:self.passWordImageView];
    // 密码
    self.passWordField = [[UITextField alloc]initWithFrame:CGRectMake(50, self.SunView.bounds.size.height / 2, self.SunView.bounds.size.width - 50, self.SunView.bounds.size.height / 2)];
    _passWordField.placeholder = @"请输入密码";
    _passWordField.delegate = self;
    _passWordField.backgroundColor = [UIColor whiteColor];
    _passWordField.clearButtonMode = UITextFieldViewModeAlways;
    _passWordField.keyboardType = UIKeyboardTypeDefault;
    _passWordField.returnKeyType = UIReturnKeyDone;
    [self.SunView addSubview:self.passWordField];
    // 倒圆角
    CALayer *layer = self.SunView.layer;
    [layer setMasksToBounds:YES];
    [layer setCornerRadius:10];    
}
// 键盘Return/Done方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (self.nickNameField.text.length == 0) {
        [self.nickNameField becomeFirstResponder];
    } else {
        [self.loginView NetWork];
    }
    return YES;
}
// 输入框开始编辑
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    // 判断第一响应者
    if ([self.nickNameField isFirstResponder]) {
        // 更改图片
        self.imgView.image = [UIImage imageNamed:@"1"];
        // 更改自身图片的颜色(假数据)
        self.nickNameImageView.image = [UIImage imageNamed:@"IMG_0423"];
        // 恢复其他图片的颜色(假数据)
        self.passWordImageView.image = [UIImage imageNamed:@"IMG_0420"];
    } else if ([self.passWordField isFirstResponder]) {
        self.imgView.image = [UIImage imageNamed:@"2"];
        self.passWordImageView.image = [UIImage imageNamed:@"IMG_0424"];
        self.nickNameImageView.image = [UIImage imageNamed:@"IMG_0419"];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
